/**
 * HAR (HTTP Archive) 相关类型定义
 */

// HAR 文件基础结构
export interface HarData {
  log: {
    version: string;
    creator: {
      name: string;
      version: string;
    };
    entries: HarEntry[];
  };
}

// HAR 条目结构
export interface HarEntry {
  startedDateTime: string;
  request: {
    method: string;
    url: string;
    headers: Array<{ name: string; value: string }>;
    queryString: Array<{ name: string; value: string }>;
    cookies: Array<{ name: string; value: string }>;
    headersSize: number;
    bodySize: number;
  };
  response: {
    status: number;
    statusText: string;
    headers: Array<{ name: string; value: string }>;
    cookies: Array<{ name: string; value: string }>;
    content: {
      size: number;
      mimeType: string;
      text?: string;
    };
    redirectURL: string;
    headersSize: number;
    bodySize: number;
  };
  timings: {
    blocked?: number;
    dns?: number;
    connect?: number;
    send: number;
    wait: number;
    receive: number;
    ssl?: number;
  };
  time: number;
  cache: {
    beforeRequest?: {
      expires?: string;
      lastAccess?: string;
      eTag?: string;
      hitCount?: number;
    };
    afterRequest?: {
      expires?: string;
      lastAccess?: string;
      eTag?: string;
      hitCount?: number;
    };
  };
  serverIPAddress?: string;
  connection?: string;
  comment?: string;
}

// 解析后的条目数据
export interface ParsedEntry {
  url: string;
  method: string;
  status: number;
  contentType: string;
  size: number;
  time: number;
  domain: string;
  resourceType: ResourceType;
  timings: {
    dns: number;
    connect: number;
    ssl: number;
    send: number;
    wait: number;
    receive: number;
    total: number;
  };
  cacheStatus: CacheStatus;
}

// 资源类型枚举
export enum ResourceType {
  DOCUMENT = 'document',
  STYLESHEET = 'stylesheet',
  IMAGE = 'image',
  MEDIA = 'media',
  FONT = 'font',
  SCRIPT = 'script',
  XHR = 'xhr',
  FETCH = 'fetch',
  WEBSOCKET = 'websocket',
  MANIFEST = 'manifest',
  OTHER = 'other'
}

// 缓存状态枚举
export enum CacheStatus {
  HIT = 'hit',
  MISS = 'miss',
  PARTIAL = 'partial',
  UNKNOWN = 'unknown'
}

// 存储的 HAR 文件结构
export interface HarFile {
  id: string;
  name: string;
  uploadTime: Date;
  size: number;
  data: HarData;
  parsedData?: ParsedEntry[];
}

// 性能指标接口
export interface PerformanceMetrics {
  // 时间指标
  totalTime: number;
  averageResponseTime: number;
  maxResponseTime: number;
  minResponseTime: number;
  p95ResponseTime: number;
  
  // 资源指标
  totalRequests: number;
  totalSize: number;
  averageSize: number;
  cacheHitRate: number;
  
  // 网络指标
  averageDnsTime: number;
  averageConnectTime: number;
  averageSslTime: number;
  averageTtfb: number;
  averageRedirectTime: number;
  averageDownloadTime: number;
  
  // 按资源类型分组
  resourceTypeStats: Record<ResourceType, {
    count: number;
    totalSize: number;
    averageTime: number;
  }>;
}

// 对比结果接口
export interface ComparisonResult {
  baseline: {
    file: HarFile;
    metrics: PerformanceMetrics;
  };
  target: {
    file: HarFile;
    metrics: PerformanceMetrics;
  };
  differences: {
    timeImprovement: number; // 时间改善百分比
    sizeReduction: number; // 大小减少百分比
    requestReduction: number; // 请求减少百分比
    cacheImprovement: number; // 缓存命中率改善
  };
  summary: {
    overallImprovement: number; // 总体改善评分
    keyInsights: string[]; // 关键洞察
    recommendations: string[]; // 优化建议
  };
}

// 筛选器接口
export interface FilterOptions {
  domains?: string[];
  resourceTypes?: ResourceType[];
  statusCodes?: number[];
  timeRange?: {
    min: number;
    max: number;
  };
  sizeRange?: {
    min: number;
    max: number;
  };
}

// 排序选项
export interface SortOptions {
  field: keyof ParsedEntry;
  direction: 'asc' | 'desc';
}

// 导出格式枚举
export enum ExportFormat {
  JSON = 'json',
  CSV = 'csv',
  PDF = 'pdf',
  HTML = 'html'
}
